/***************************************************************************
 *   Copyright (C) 2009 by pawel   *
 *   pawel@byszewski.pl   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
*
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <fcntl.h>
#include <string>
#include "recdes.h"
#define DESLENGTH 14
#define ELEMENTCOUNT 5


using namespace std;


void resizeDesFile(int fd){
	int dl,pos;
	dl=lseek(fd, (10*DESLENGTH)-1, SEEK_END);
	cout <<"w funkcji: "<<dl;
	pos=write(fd, "q", 1);
	//cout <<"       po write: "<<pos <<endl;
	//dl=lseek(fd, 0,SEEK_END);
	//cout <<" w funkcji 2: "<<dl<<endl;
}

int main(int argc, char *argv[])
{
	
	int fd=0;
	int dl=0;
	int pos=0;
	char a = 'a';
	int  c=123;
	int  c1=0;
	char  buf[20];
	RecordDescriptor *rd =new RecordDescriptor();
		rd->setBooked(1);
		rd->setSize(30);
		rd->setOfset(0);
		rd->setType(0,1);rd->setType(1,3);rd->setType(2,2);rd->setType(3,2);rd->setType(4,3);
	RecordDescriptor *rd1 =new RecordDescriptor();
		rd1->setBooked(0);
		rd1->setSize(45);
		rd1->setOfset(31);
		rd1->setType(0,1);rd1->setType(1,0);rd1->setType(2,1);rd1->setType(3,2);rd1->setType(4,1);
	//char buff[4];
	char *buff;
	buff=(char *)&c;
	//cout<<"----*"<<buff[0]<<"*"<<buff[1]<<"*"<<buff[2]<<"*"<<buff[3]<<endl;
	int *cf;
	cf=(int *)buff;
	//cout <<"cf: "<<*cf<<endl;
	
	
	//Settings *s= Settings.getInstance();
 
	fd=open("nowy.txt", O_CREAT | O_RDWR, 07777);//domyslnie jest do pisania czytania
	resizeDesFile(fd);
	rd->wypisz();
	rd1->wypisz();
	lseek(fd,0,SEEK_SET);
	cout <<"dupa1"<<endl;
	char *save=rd->createBuf();
	cout <<"dupa2"<<endl;
	char *load = new char[14];
	//write(fd,save,DESLENGTH);
	lseek(fd,0,SEEK_SET);
	read(fd, load, 14);
	rd1->createFromBuf(load);
	rd1->wypisz();
	close(fd);
	/*/fstream file("nowy.txt", fstream::in | fstream::out);
	//file << a;
	//file << c;
	//cout << a << endl;
	//cout << c ;
	//rd->toStream(file);
	//rd1->toStream(file);

	
	cout << endl;
	string *s = new string("ala ma kota");
	string *s1 = new string("cos");
	string s2("baaaaaaaaaaaaaaaaaaaaaaaaaaaaaardzo");
	const char *buf1;
	buf1=s->c_str();
for( int i=0; i <= s->length();++i){
	cout <<"*"<<*(buf1+i);
	if(*(buf1+i)==NULL) cout <<endl <<"NULL!"<<endl;
}

	
	//write(fd, buf1, (s->length())+1);	
	//write(fd, &c,4);
	
	read(fd, buf, 12);
	for( int i=0; i <= s->length();++i){
		cout <<"*"<<*(buf+i);
		if(*(buf+i)==NULL) cout <<endl <<"NULL!";
	}
	string nowys(buf);
	cout << endl << "stworzony string: " << nowys <<"*"<<nowys.length();
	int *k;
	char bufi[4];
	read(fd, bufi, 4);
	k=(int *)bufi;
	cout << endl << "odczytany int: "<< *k << endl;
	
	char j =0;
	char l=1;
	char h=2;
	char g=17;
	cout <<endl<<"test charow "<< j<<"*"<< l<<"*"<<h<<"*"<<g<<"*"<< endl;
	int g1=(int)g;
	cout <<"rzutowane g: " << g1 <<endl;
	
	
	//int k=buf[1];
	//cout <<"*"<<buf[0]<<"*"<<buf[1]<<"*"<<buf[2]<<"*"<<k<<endl;
	//s1=(string *)buf;
	//cout << "string: "<< *s1;
	/*
	//write(fd, &c, 4);
	read(fd, buf, 4);
	cout <<"*"<< buf[0] <<"*"<< buf[1] <<"*"<< buf[2] <<"*"<< endl;
	int *c2;
	c2=(int *) buf;
	//cout << s->
	//file << c;
	//file.flush();
	//file >> c1;
	cout << "c2: " << *c2<< endl;
	////file << c;*/
	//file.close();
	//close(fd);
	
	
  //cout <<"po: " <<dl<<"  "<<pos <<endl;

  return EXIT_SUCCESS;
}

